<?php
	//周报数据
	//error_reporting(0);
	set_time_limit(E_ALL);
	include 'simplehtmldom/simple_html_dom.php';
	include_once(dirname(__FILE__).'/dbutil.php');
	function saveRecord($table,$record,$db){
    	if(empty($table))
    		return false;
    	if(!is_array($record) || empty($record))
    		return false;
    	//$db = new DBUtil();
    	//$this->initDB();
    	$r = array();
    	foreach($record as $key=>$item){
    		$r[$db->escape($key)] = $db->escape($item);
    	}
    	$cols = implode("," , array_keys($r));
    	$values = implode("','" , $r);
    	$sql = "insert into " . $db->escape($table) . " (" . $cols . ") values ('" . $values . "')";
    	return $db->query($sql) ? $db->getlastid() : false;
    }
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
	// 房天下
/*	$baseUrl = 'http://newhouse.macau.fang.com';
	$url = $baseUrl . '/house/s/';*/
	//$url = 'http://newhouse.wuzhong.fang.com/house/s/b94/';
	$url='http://fdc.fang.com/data/house/house_bj.htm';
	while(true){
		$recordList = array();
		// 西安房天下
		$listPage = file_get_html('compress.zlib://'.$url);
		//echo($listPage);exit;
		//$listPage = file_get_contents('compress.zlib://http://newhouse.xian.fang.com/house/dianshang/');
		//echo $listPage
		if(empty($listPage)){
			break;
		}
		$list = $listPage->find('div[class=morecitydown]');
	
		if(!empty($list)){
				$tmp = $list[0]->find('a');
				//有公开数据的城市链接，名字
				$cityArray=array();
				//有数据的城市链接
				for($a=0;$a<count($tmp);$a++){
					if($tmp[$a]->href !== '/creisdata/'){
						$name = trim(mb_convert_encoding($tmp[$a]->innertext,'utf-8','gbk'));
						$qz="http://fdc.fang.com";
					//	echo $name.":".$tmp[$a]->href.'<br>';
						$cityArray[$a]=array('city_name'=>$name,'city_link'=>$qz.$tmp[$a]->href);
					}
				}
			//	var_dump($cityArray);

				$recordList=array();	
				//抓取每个城市的数据

			foreach($cityArray as $cityitem){
				        $time=date('Y-m-d H:i:s');//抓取数据时间
					$url=$cityitem['city_link'];//$cityArray[$a]['city_link'];
					$detailcity = file_get_html('compress.zlib://'.$url);
					$tbdata = $detailcity->find('table[class=ltab01]');
					$trdata=$tbdata[0]->find('tr');
					//首次抓取全部
					foreach($trdata as $dd){
					    $tddata=$dd->find('td');
					    if(!empty($tddata)){
						    $tdArray=array();
						    	for($b=0;$b<3; $b++){
								$tdArray['city']=$cityitem['city_name'];//$cityArray[$a]['city_name'];
								$tdArray['time']=$time;
								$z = trim(mb_convert_encoding($tddata[$b]->innertext,'utf-8','gbk'));
								switch($b){
									case 0:
										$tdArray['week']=$z;//周次
										$tdArray['year']=substr($z,0,4);//月份
										break;
									case 1:
										$tdArray['number']=$z;//成交套数（套）
										break;
									case 2:
										$tdArray['area']=$z;//成交面积（万㎡）
										break;
								}
						  	}
						  	$recordList[]=$tdArray;
					 	} 
					}
					//每周抓取，只取一条
				/*	$tddata=$trdata[1]->find('td');
					$tdArray=array();
					for($b=0;$b<3; $b++){
						$tdArray['city']=$cityArray[$a]['city_name'];
						$tdArray['time']=$time;
						$z = trim(mb_convert_encoding($tddata[$b]->innertext,'utf-8','gbk'));
						switch($b){
							case 0:
								$tdArray['week']=$z;//周次
								$tdArray['year']=substr($z,0,4);//月份
								break;
							case 1:
								$tdArray['number']=$z;//成交套数（套）
								break;
							case 2:
								$tdArray['area']=$z;//成交面积（万㎡）
								break;
						}
					}
					$recordList[]=$tdArray;
					*/
					
				}

		//	var_dump($recordList);//exit;
			$db = new DBUtil();
			$mm=0;
			foreach($recordList as $item){
				$mm+=1;
				$h = $db->getOneRow("select id from t_residence_weekly where city = '".$db->escape($item['city'])."' and week='".$db->escape($item['week'])."'");
				if(empty($h)){
					$cid=$h = $db->getOneRow("select c.cityID,c.father from hat_city c left join hat_province p on c.father=p.provinceID where  p.province like'%".$db->escape($item['city'])."%' or c.city like'%".$db->escape($item['city'])."%'");
					$item['cityId']=$cid['cityID'];
					// 保存周报信息
					saveRecord('t_residence_weekly',$item,$db);
					echo 'success'.$mm.'<br>';
				}
			}
			$db->close();
			echo 'finish';
			exit;
		}
		/*
		$nextPage = $listPage->find('div[class=page]')[0]->find('a[class=active]')[0]->next_sibling();
		echo ($nextPage->href.'<br/>');
		if(!empty($nextPage) ){
			$url = $baseUrl . $nextPage->href;
		}else{
			break;
		}*/
		//sleep(30);
		//echo $url;break;
	}
	
	// 删除html
	function removeHtml($value){
		return preg_replace('/<.*?<\/\w+>/','',$value);
	}
	// 删除全部空格
	function removeSpace($value){
		return preg_replace('/\s+/','',$value);
	}
?>